package com.zxy.codeserver;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.zxy.common.CodeRemoteCall;
import com.zxy.common.User;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author zhangxuyuan
 * @version 1.0
 * @date 2021/1/27
 */
@Service
public class CodeService implements CodeRemoteCall {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Integer validate(String email,String code)
    {
        User user = (User)redisTemplate.opsForValue().get(email);
        if(user==null)
        {
            return 1;
        }
        String checkcode = user.getCheckcode();
        DateTime expireTime = user.getExpireTime();
        if(!StringUtils.equals(checkcode,code))
        {
            return 1;
        }
        if(DateUtil.compare(DateUtil.dateSecond(),expireTime)>0)
        {
            return 2;
        }
        return 0;
    }
}
